<template>
    <div>
        <el-table :data="localTasks" size="mini" border :style="{ width: '100%', marginTop: '15px' }" row-key="id">
            <el-table-column prop="userName" label="执行人" width="100">
                <template slot-scope="scope">
                    <div>
                        {{ scope.row.receiveUserName }}
                        <el-button type="text" @click="setTaskExecuteUser(scope.row)" v-if="handleing" size="mini"> {{ scope.row.receiveUser != null ? "修改" : "设置" }}</el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="taskDeptName" label="所属部门" width="120"> </el-table-column>
            <el-table-column prop="taskContent" label="任务内容">
                <template slot-scope="scope">
                    <FlashTaskModule ref="flashTaskModule" :needFlash="scope.row.urgent == 1 && scope.row.status == 1" :taskContent="scope.row.taskContent" />
                </template>
            </el-table-column>
            <el-table-column prop="helpContent" label="辅助信息"> </el-table-column>
            <el-table-column prop="helpContent" label="任务时限">
                <template slot-scope="scope">
                    <TaskDDL :taskId="scope.row.id" :ddl="scope.row.ddl" @handleOk="handleDDLSet" />
                </template>
            </el-table-column>
            <el-table-column prop="taskContent" label="任务状态" align="center" width="100">
                <template slot-scope="scope">
                    <el-tag type="warning" v-if="scope.row.status == 0" size="mini">待下发</el-tag>
                    <el-tag v-if="scope.row.status == 1" size="mini">未完成</el-tag>
                    <el-tag type="success" v-if="scope.row.status == 2" size="mini">已完成</el-tag>
                    <el-tag type="danger" v-if="scope.row.status == 3" size="mini">已终止</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="190" v-if="handleing">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" v-if="scope.row.status != 2" @click="handleTaskEdit(scope.row)">编辑</el-button>
                    <el-button type="text" size="mini" v-if="scope.row.status == 0 || scope.row.status == 1" @click="handleStopTask(scope.row)">{{ scope.row.status == 0 ? "删除" : "取消" }}</el-button>
                    <el-button type="text" size="mini" v-if="scope.row.receiveUser != null" @click="chat(scope.row)">任务沟通</el-button>
                    <el-button type="text" size="mini" v-if="scope.row.receiveUser != null" @click="handleTaskNotice(scope.row)">任务提醒</el-button>
                </template>
            </el-table-column>
        </el-table>
        <StaffChooseDialog ref="staffChoose" :muilt="false" @Unitpersonnel="onStaffChoose" />
        <ChatIM ref="chatIM" />
        <TaskWarn ref="taskWarnRef" />
    </div>
</template>

<script>
import { updateReceiveUser, getHandleLevelTasks, stopTask } from "@/api/response/handle.js";
import TaskSubmitView from "@/views/components/taskSubmitView/index.vue";
import StaffChooseDialog from "@/views/components/staffChooseDialog/index.vue";
import TaskDDL from "./TaskDDL.vue";
import { setTaskTimeLimit } from "@/api/response/task";
import chatMixin from "@/mixins/chatMixin";
import ChatIM from "@/components/ChatIM/task.vue";
import TaskWarn from "../components/TaskWarn.vue";
import * as Auth from "@/utils/auth";
import FlashTaskModule from "./FlashTaskModule.vue";

export default {
    name: "HandleTaskTableView",
    components: { TaskSubmitView, StaffChooseDialog, TaskDDL, ChatIM, TaskWarn, FlashTaskModule },
    mixins: [chatMixin],
    props: {
        tasks: {
            type: Array,
            default: [],
        },
        handleId: {
            type: Number,
            default: 0,
        },
        levelId: {
            type: Number,
            default: 0,
        },
        time: { type: Number, default: 1000 * 60 * 0.1 },
        handleing: {
            type: Boolean,
            default: true,
        },
    },
    data() {
        return {
            timer: null,
            localTasks: [],
            taskId: 0,
        };
    },
    mounted() {
        if (this.tasks.length > 0) {
            this.localTasks = this.tasks;
        }
    },
    methods: {
        getTasks() {
            if (this.handleId != 0 && this.levelId != 0) {
                getHandleLevelTasks({
                    handleId: this.handleId,
                    levelId: this.levelId,
                }).then(res => {
                    this.localTasks = res.data;
                    this.$emit("refreshCurrentTasks");
                });
            }
        },
        setTaskExecuteUser(data) {
            this.taskId = data.id;
            this.$refs.staffChoose.showDialog("选择任务执行人");
        },
        onStaffChoose(deptId, userId, user, dept) {
            console.log(deptId, userId, user, dept);
            // 调用设置执行人接口，只有应急处置才需要修改这块的设置，所以直接默认设置就行了
            updateReceiveUser({
                id: this.taskId,
                receiveUser: userId,
                receiveUserName: user.userName,
                taskDept: deptId,
                taskDeptName: dept.name,
            }).then(res => {
                this.$modal.msgSuccess(res.msg);
                this.$emit("refreshCurrentTasks");
            });
        },
        handleStopTask(data) {
            // 删除或者取消任务
            this.$modal.loading(data.status == 1 ? "正在取消任务，请稍后..." : "正在删除任务，请稍后...");
            stopTask(data.id).then(res => {
                this.$modal.closeLoading();
                if (res.code == 200) {
                    this.$modal.msgSuccess(res.msg);
                } else {
                    this.$modal.msgError(res.msg);
                }
                this.$emit("refreshCurrentTasks");
            });
        },
        handleDDLSet(taskId, timelimit) {
            setTaskTimeLimit(taskId, timelimit).then(res => {
                if (res.code == 200) {
                    this.$modal.msgSuccess("任务时限修改成功");
                    this.$emit("refreshCurrentTasks");
                } else {
                    this.$modal.msgError("任务时限修改失败");
                }
            });
        },
        chat(row) {
            console.log(row);
            let uid = Auth.getUserId();
            if (row.receiveUser == uid) {
                // 不允许自己跟自己交流
                this.$modal.msgError("不能和自己沟通");
                return false;
            }
            this.initTaskChat("handle", this.handleId, row.id, row.receiveUserName);
        },
        handleTaskNotice(row) {
            this.$refs.taskWarnRef.showDialog(row);
        },
        isAllTaskHasReceiverUser() {
            let flag = true;
            this.localTasks.forEach(item => {
                if (item.receiveUser == null) {
                    flag = false;
                }
            });
            return flag;
        },
        handleTaskEdit(row) {
            this.$emit("handleTaskEdit", row);
        },
    },
    watch: {
        tasks(n, o) {
            this.localTasks = n;
        },
    },
};
</script>

<style lang="scss" scoped></style>
